package com.作业.Day20;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Homework1 {
}

/**
 * 客户端
 */
class Client {
    static Scanner scanner = new Scanner(System.in);
    static int choice = 0;

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("localhost", 9999);
//                new ClientThread(socket).start();
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());//输出给服务器
        System.out.println("请选择您想要的业务");
        System.out.println("1.登录");
        System.out.println("2.咨询");
        System.out.println("3.退出");
        choice = scanner.nextInt();
        dataOutputStream.writeUTF(choice+"");
        switch (choice) {
            case 1:
                login(socket);
                break;
            case 2:
                consult(socket);
            case 3:
                System.out.println("程序结束");
                break;
        }

    }

    private static void consult(Socket socket) throws IOException {
        while (true) {
            System.out.println("您想要咨询什么问题");
            String question = scanner.next();
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());//输出给服务器
            dataOutputStream.writeUTF(question);
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());//接收服务器数据
            System.out.println(dataInputStream.readUTF());
        }
    }

    private static void login(Socket socket) throws IOException {
        while (true) {
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            System.out.print("账号:");
            dataOutputStream.writeUTF(scanner.next());
            System.out.print("密码:");
            dataOutputStream.writeUTF(scanner.next());
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            String s = dataInputStream.readUTF();
            System.out.println(s);
            if (s.equals("登录成功")) {
                break;
            }
        }
    }
}
//class ClientThread extends Thread{
//    private Socket socket;
//
//    public ClientThread(Socket socket) {
//        this.socket = socket;
//    }
//    public void run() {
//        while (true){
//            try {
//                DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
//                String s = dataInputStream.readUTF();
//                System.out.println(s);
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//
//        }
//    }
//}

/**
 * 服务端
 */
class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket accept = serverSocket.accept();
        new ServerThread(accept).start();

    }
}

class ServerThread extends Thread {
    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        while (true) {
             try {
                 DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());//客户端输入
                 DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());//客户端输出
                 int choice = Integer.parseInt(dataInputStream.readUTF());
                 //登录验证·
                 if (choice == 1) {
                     String account = dataInputStream.readUTF();
                     String password = dataInputStream.readUTF();
                     if (account.equals("admin") && password.equals("123456")) {
                         System.out.println("客户端登录成功");
                     }

                     dataOutputStream.writeUTF("登录成功");
                 }
                 //咨询
                 if (choice == 2) {
                     String question = dataInputStream.readUTF();
                     System.out.println("咨询问题：" + question);
                     System.out.print("请回答:");
                     String next = Client.scanner.next();
                     dataOutputStream.writeUTF(next);
                 }
             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
        }

    }
}

